package com.example.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitmqUtil {
    private  static ConnectionFactory connectionFactory;
    static {
        connectionFactory = new ConnectionFactory();
        /* 创建连接IP */
        connectionFactory.setHost("127.0.0.1");
        /* 创建连接端口号 */
        connectionFactory.setPort(5672);
        /* 创建连接虚拟主机 */
        connectionFactory.setVirtualHost("/ems");
        /* 创建连接用户名和密码 */
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("123");
    }
    public static Connection getConnection(){
        try {

            /* 获取连接对象 */
            return connectionFactory.newConnection();
        }catch ( Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static void closeConnectionAndChanel(Channel channel,Connection connection){
        try {
            if(channel!=null)channel.close();
            if(connection!=null)connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
